﻿using LxBasic;
using System;
using System.Collections.Generic;
using System.Diagnostics.SymbolStore;
using System.Text;
using System.Threading.Tasks;

namespace LxLESS
{
    public class LESSZone
    {
        public bool IsSymmetric { get; set; } = false;
        public List<MGZone> MGZones { get; set; } = new List<MGZone>();
        public int MaxCoarseLevel { get; set; } = -1;
        public BiCGStabZone BiCGZone { get; set; }

        public RMSResidualTool Residual = new RMSResidualTool();
        public void checkTopology()
        {
            Console.WriteLine("粗网格层数：{0} maxCoarse:{1}", MGZones.Count, MaxCoarseLevel);
            for (int i1 = 0; i1 < MGZones.Count; i1++)
            {
                Console.WriteLine("第 {0} 层粗网格：Cell数量{1}，Face数量{2}。", i1, MGZones[i1].InnCells.Length, MGZones[i1].InnFaces.Length);
            }
        }
        public void CheckCreate(IBuildMGZone startZone, int mergeCellForAMG, int varLengthForBicg = -1, List<double[]> useShareArray = null)
        {
            //创建多重网格
            MGZones.Clear();
            MGZoneCreator zoneCreator = new MGZoneCreator();
            MGZone mgzone = zoneCreator.CreateMGZone(startZone, mergeCellForAMG);
            MGZones.Add(mgzone);
            MaxCoarseLevel = 0;
            while (MGZones.Count < AMGGlobal.MaxLevel)
            {
                MGZone fineZone = MGZones.Last();
                MGZone coarseZone = zoneCreator.CreateMGZone(fineZone, mergeCellForAMG);
                if (fineZone.InnCells.Length > coarseZone.InnCells.Length)
                {
                    MaxCoarseLevel = MGZones.Count;
                }
                MGZones.Add(coarseZone);
            }
            //shareArray校核
            varLengthForBicg = Math.Max(varLengthForBicg, MGZones[0].InnCells.Length);
            if (useShareArray == null)
            {
                useShareArray = new List<double[]>();
            }
            if (useShareArray.Count == 0)
            {
                useShareArray.Add(new double[varLengthForBicg]);
            }
            if (useShareArray[0].Length < varLengthForBicg)
            {
                useShareArray[0] = new double[varLengthForBicg];
            }
            //创建mg存储
            foreach (var cz in MGZones)
            {
                cz.CreateArray(this, useShareArray[0]);
            }
            //创建bicg存储区
            BiCGZone = new BiCGStabZone();
            BiCGZone.CheckCrete(varLengthForBicg, useShareArray);
        }
        public void CheckCreate(IBuildMGZone startZone)
        {
            CheckCreate(startZone, 4, MGZones[0].InnCells.Length);
        }

    }
}